'use strict';

const Controller = require('egg').Controller;

const {createId,createToken,createCode}=require("../utils/utils")
const path=require("path")
const fs=require("fs")
const {sendMail}=require("../utils/sendMail")

class HomeController extends Controller {

  // 邮箱
  async getEmail(){
    const {ctx}=this
    const {email}=ctx.request.body
    const result=await sendMail(email);
    if(result.code==200){
      ctx.body={
        code:"1001",
        msg:"发送邮箱成功"
      }
    }else{
      ctx.body={
        code:"1002",
        msg:"发送邮箱失败"
      }
    }
  }

  // oss;
  async getOss(){
    const {ctx}=this
    const file = ctx.request.files[0];
    const name = '1907C'+new Date()*1 + path.basename(file.filename);
    let result;
    try {
      result = await ctx.oss.put(name, file.filepath);
    } finally {
      await fs.unlinkSync(file.filepath);
    }
 
    if (result) {
      if(result.res.status==200){
        const res=await ctx.service.user.getOss({
          id:ctx.request.query.id,
          imgurl:result.url
        })
        if(res.affectedRows){
          ctx.body={
            code:"1001",
            msg:"oss上传成功"
          }
        }else{
          ctx.body={
            code:"1002",
            msg:"oss上传失败"
          }
        }
      }
    } else {
      ctx.body = 'please select a file to upload！';
    }
  }

  // 获取用户信息
  async getAccess(){
    const {ctx}=this
    const {access_token}=ctx.request.query;
    console.log(access_token);
    const result=await ctx.service.user.getAccess(access_token)
    const token=createToken(result.data.name)
    if(result.status==200){
     ctx.body={
       code:"1001",
       msg:'获取用户信息成功',
       data:result.data,
       token
     } 
    }else{
      ctx.body={
        code:"1002",
        msg:"获取用户信息失败"
      }
    }
  }

  // 第三方登录
  async getCodes(){
    const {ctx}=this
    const {code}=ctx.request.query
    const result=await ctx.service.user.getCodes(code);
    if(result.status==200){
      ctx.body={
        code:"1001",
        msg:"第三方登录成功",
        data:result.data,
        isAbandon:true
      }
    }else{
      ctx.body={
        code:"1002",
        msg:"第三方登录失败"
      }
    }
  }

  // 模糊搜索
  async filter(){
    const {ctx}=this
    const {text}=ctx.request.query
    const result=await ctx.service.user.filter(text)
    if(result.length){
      ctx.body={
        code:"1001",
        msg:'搜索数据成功',
        result
      }
    }else{
      ctx.body={
        code:"1002",
        msg:"搜索数据失败"
      }
    }
  }

  // 删除;
  async destroy(){
    const {ctx}=this;
    const {id}=ctx.params
    const result=await ctx.service.user.destroy(id)
    if(result.affectedRows){
      ctx.body={
        code:"1001",
        msg:'删除成功'
      }
    }else{
      ctx.body={
        code:"1002",
        msg:"删除失败"
      }
    }
  }

  // 查看
  async show(){
    const {ctx}=this
    const {id}=ctx.params
    const result=await ctx.service.user.show(id);
    if(result.length){
      ctx.body={
        code:"1001",
        msg:'查看数据成功',
        result
      }
    }else{
      ctx.body={
        code:"1002",
        msg:"查看数据失败"
      }
    }
  }

  // 编辑
  async update(){
    const {ctx}=this
    const {id}=ctx.params
    const {username,nickname,age}=ctx.request.body
    const result=await ctx.service.user.update(id,username,nickname,age)
    if(result.affectedRows){
      ctx.body={
        code:"1001",
        msg:'编辑成功'
      }
    }else{
      ctx.body={
        code:"1002",
        msg:"编辑失败"
      }
    }
  }

  // 增加
  async create(){
    const {ctx}=this
    const {username,nickname,age}=ctx.request.body
    const id=createId(username)
    const result=await ctx.service.user.create(id,username,nickname,age)
    if(result.affectedRows){
      ctx.body={
        code:"1001",
        msg:'添加成功'
      }
    }else{
      ctx.body={
        code:"1002",
        msg:"添加失败"
      }
    }
  }

  // 获取数据;
  async index(){
    const {ctx}=this
    const result=await ctx.service.user.index()
    if(result.length){
      ctx.body={
        code:"1001",
        msg:'获取数据成功',
        result
      }
    }else{
      ctx.body={
        code:"1002",
        msg:"获取数据失败"
      }
    }
  }

  // 注册;
  async registry(){
    const {ctx}=this
    const {username,pwd}=ctx.request.body
    const flag=await ctx.service.user.chengName(username)
    if(!flag.length){
      const id=createId(username)
      const result=await ctx.service.user.registry(id,username,pwd)
      if(result.affectedRows){
        ctx.body={
          code:"1001",
          msg:"注册成功"
        }
      }else{
        ctx.body={
          code:"1002",
          msg:"注册失败"
        }
      }
    }else{
      ctx.body={
        code:"1002",
        msg:"该用户名已注册"
      }
    }
  }

  // 验证码;
  async getCode(){
    const {ctx}=this
    const code=createCode()
    ctx.session.code=code.text
    ctx.body={
      code:"1001",
      msg:"验证码获取成功",
      text:code
    }
  }

  // 登录
  async login() {
    const { ctx } = this
    const {username,pwd,code}=ctx.request.body
    // if(ctx.session.code==code){
      const result=await ctx.service.user.login(username,pwd)
      if(result.length){
        const token=createToken(result[0].id)
        ctx.body={
          code:"1001",
          msg:"登录成功",
          token,
          id:result[0].id,
          type_id:result[0].type_id
        }
      }else{
        ctx.body={
          code:"1002",
          msg:"登录失败"
        }
      }
    // }else{
    //   ctx.body={
    //     code:'1002',
    //     msg:"验证码错误"
    //   }
    // }
  }
}

module.exports = HomeController;
